package org.utils.io;

import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.PrintStream;

public class Head
{
   public static final int DEFAULT_LINE_COUNT = 10;

   public static void head(String filename) throws IOException
   {
   	head(System.out, filename);
   }

   public static void head(String filename, int lines) throws IOException
   {
   	head(System.out, filename, lines);
   }

   public static void head(PrintStream out, String filename) throws IOException
   {
	head(out, filename, DEFAULT_LINE_COUNT);
   }

   public static void head(PrintStream out, String filename, int lines) throws IOException
   {
	LineNumberReader in = new LineNumberReader( new FileReader(filename) );
	for (int i = 0; i < lines; i++)
	{
		out.println( in.readLine() );
	}
	in.close();
   }

   public static void main(String[] args) throws IOException
   {
   	if (args.length == 0)
		System.err.println("Missing filename");
	if (args.length == 2)
		head(args[0], Integer.parseInt(args[1]));
	else
		head(args[0]);
   }
}
